package com.aidex.web.controller.biz;

import com.aidex.biz.domain.Order;
import com.aidex.biz.domain.RefundOrder;
import com.aidex.biz.service.OrderService;
import com.aidex.biz.service.RefundOrderService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 售后订单Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-07-02
 */
@RestController
@RequestMapping("/biz/bizRefundOrder")
public class RefundOrderController extends BaseController
{
    @Autowired
    private RefundOrderService refundOrderService;

    @Autowired
    private OrderService orderService;

    /**
     * 查询售后订单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizRefundOrder:list')")
    @GetMapping("/list")
    public R<PageInfo> list(RefundOrder refundOrder, HttpServletRequest request, HttpServletResponse response)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            refundOrder.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
         }
        refundOrder.setPage(new PageDomain(request, response));
        return R.data(refundOrderService.findPage(refundOrder));
    }

    /**
     * 审核售后订单 微信退款
     */

    @Log(title = "审核售后订单", businessType = BusinessType.UPDATE)
    @PostMapping("/wxRefunds")
    public R wxRefunds(@RequestBody RefundOrder refundOrder)
    {
        refundOrder=refundOrderService.get(refundOrder.getOrderId());
         R r=refundOrderService.wxRefunds(refundOrder);
         if(r.getCode()==200){
             Order order=orderService.get(refundOrder.getOrderId());
             order.setStatus("10");
             orderService.save(order);
         }
        return refundOrderService.wxRefunds(refundOrder);
    }


    /**
     * 导出售后订单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizRefundOrder:export')")
    @Log(title = "售后订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(RefundOrder refundOrder)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    refundOrder.setCompanyId(null);
            }else{
                         refundOrder.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<RefundOrder> list = refundOrderService.findList(refundOrder);
        ExcelUtil<RefundOrder> util = new ExcelUtil<RefundOrder>(RefundOrder.class);
        return util.exportExcel(list, "售后订单数据");
    }

}
